package com.bocai.kelala.wxapi;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.bocai.kelala.R;
import com.bocai.kelala.bean.MessageEvent;
import com.bocai.kelala.pay.simcpux.Constants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import org.greenrobot.eventbus.EventBus;


public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler {
	
	private static final String TAG = "MicroMsg.SDKSample.WXPayEntryActivity";
	
    private IWXAPI api;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pay_result);
        
    	api = WXAPIFactory.createWXAPI(this, Constants.APP_ID);
        api.handleIntent(getIntent(), this);
    }

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		setIntent(intent);
        api.handleIntent(intent, this);
	}

	@Override
	public void onReq(BaseReq req) {

	}

	@Override
	public void onResp(BaseResp resp) {

		if (resp.getType() == ConstantsAPI.COMMAND_PAY_BY_WX) {
			Log.e("tag", String.valueOf(resp.errCode));
			switch (resp.errCode) {
				case 0://微信支付成功
					EventBus.getDefault().post(new MessageEvent("11"));
					Toast.makeText(this, "支付成功", Toast.LENGTH_SHORT).show();
					break;
				case -1:
					// 支付失败

					Toast.makeText(this, "微信支付失败", Toast.LENGTH_SHORT).show();
					break;
				case -2:
					// 取消支付
					Toast.makeText(this, "微信支付取消", Toast.LENGTH_SHORT).show();
					break;
			}
			finish();
		}
	}
}